<template>
    <div>
        <Table border stripe :columns="header" :data="data_list" size="small" ref="table" class="table" @on-selection-change='rowSelect' height="300"></Table>
    </div>
</template>
<script>
export default {
    data() {
        return {

            //初始化url
            init_url: '/mes/core/app/real/getExclude/list/',

            //应用添加url
            app_add_url: '/mes/core/app/real/addApp',

            //表格标题
            header: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    type: 'index',
                    width: 60,
                    align: 'center'
                },
                {
                    "title": "应用名称",
                    "key": "name",
                    "width": 120,
                    "sortable": true,
                },
                {
                    "title": "创建时间",
                    "key": "createTime",
                    "width": 120,
                    "sortable": true
                },
                {
                    "title": "备注",
                    "key": "remark",
                    "min-width": 150,
                    "sortable": true
                },

            ],
            ruleValidate: {
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
            },
            //表格数据源
            data_list: [],
            //表格数据源缓存
            data_cache_list: [],
            //选中的数据的应用id集合
            data_select_list: [],
            //查询的名称
            insName: '',

        }
    },
    methods: {
        //到处excel
        exportData(type) {
            if (type === 1) {
                this.$refs.table.exportCsv({
                    filename: '原始数据'
                });
            } else if (type === 2) {
                this.$refs.table.exportCsv({
                    filename: '排序和过滤后的数据',
                    original: false
                });
            }
        },

        //根据学院名称查询学院
        findByInsName() {
            this.insName = this.$stringUtil.stringTrim(this.insName);
            var name = this.insName;

            if (name == "") {
                this.data_list = this.data_cache_list.slice();
                return;
            }

            var match = this.$stringUtil.stringMatch;
            this.data_list = $.grep(this.data_cache_list, function (value) {
                if (match(value.name, name)) {
                    return value;
                }
            });
        },
        //选择变化的触发事件
        rowSelect(selection) {

            if (selection && selection.length > 0) {
                var arr = [];
                for (var index in selection) {
                    arr.push(selection[index].id);
                }
                this.data_select_list = arr;

            } else {
                this.data_select_list = [];
            };
            this.$emit('getSelectValue', this.data_select_list);
        },
        //将应用添加至学院
        addApp() {
            var _self = this;
            var func = this.appinit;
            console.log(_self.data_select_list)
            if (this.data_select_list && this.data_select_list.length > 0) {

                this.$ajax.post(_self.app_add_url, {
                    appIdList: _self.data_select_list,
                    insId: _self.insId
                })
                    .then(function (response) {
                        if (response.data.status == "success") {
                            _self.$Message.info("添加成功");
                            func();
                        } else {
                            _self.$Message.info("应用添加失败");
                        }
                    })
                    .catch(function (error) {
                        _self.$Message.info("应用添加失败");
                    })
            } else {
                _self.$Message.info("添加失败，未选择应用！");
            }

        },
        appinit(insId) {
            var _self = this;
           
            this.$ajax.get(_self.init_url + insId)
                .then(function (response) {
                    _self.data_list = response.data.data;
                    _self.data_cache_list = _self.data_list.slice();

                })
                .catch(function (error) {
                    console.log(error);
                });
        },

    },
    mounted() {
        // var _self = this;
        // //初始化表单数据
        // this.$ajax.get(_self.init_url + _self.insId)
        //     .then(function (response) {
        //         _self.data_list = response.data;
        //         _self.data_cache_list = _self.data_list.slice();
        //     })
        //     .catch(function (error) {
        //         console.log(error);
        //     });
    },
    props: ['insId'],
    watch: {
        insId: function (newValue, oldValue) {
            var _self = this;
            //初始化表单数据
            // this.$ajax.get(_self.init_url + newValue)
            //     .then(function (response) {
            //         _self.data_list = response.data;
            //         _self.data_cache_list = _self.data_list.slice();
            //     })
            //     .catch(function (error) {
            //         console.log(error);
            //     });
        }
    }
}
</script>

<style>
.btn-group {
    margin-bottom: 5px;
}
</style>
